package com.qf.smsplatform.mq;

import com.qf.smsplatform.common.constants.RabbitMQConstants;
import com.qf.smsplatform.common.model.StandardSubmit;
import com.qf.smsplatform.executor.StrategyExecutor;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class PreSendListener {

    @Autowired
    private StrategyExecutor executor;

    @RabbitListener(queues = RabbitMQConstants.TOPIC_PRE_SEND)
    public void consume(StandardSubmit submit, Channel channel, Message message) throws IOException {
        log.info("【策略模块-PreSendListener】 接收到 {} 队列中的消息， submit = {}",RabbitMQConstants.TOPIC_PRE_SEND,submit);
        //1. 调用executor执行策略链
        executor.execute(submit);
        //2. 手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        log.info("【策略模块-PreSendListener】 消费完毕 submit = {}",submit);
    }
}
